package com.metrics.graphics;

import static com.metrics.utils.GraphicUtil.*;

import java.awt.Color;
import java.io.*;
import java.util.List;

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.data.category.*;
import org.jfree.data.general.AbstractDataset;

import com.database.objects.*;
import com.metrics.extractors.MetricExtractor;
import com.metrics.formatters.TimeFormatter;

public class GraphicBarGenerator extends GraphicGenerator {

	private static final String LEGEND_X = "Developer";

	private static final String LEGEND_Y = "Contribution";

	@Override
	public AbstractDataset getData(List<Developer> developers, MetricExtractor extractor, Type type, Status status, String start, String finish, TimeFormatter formatter) {
		DefaultCategoryDataset data = new DefaultCategoryDataset();
		for (Developer developer : developers) {
			data.addValue(developer.getValue(), developer.getName(), "");
		}
		return data;
	}

	@Override
	public void graphic(OutputStream out, AbstractDataset data) throws IOException {
		JFreeChart chart = ChartFactory.createBarChart("", LEGEND_X, LEGEND_Y, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, true);
		decorateBarChart(chart);
		ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT);
	}

	private void decorateBarChart(JFreeChart chart) {
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.WHITE);
		plot.setRangeGridlinePaint(Color.BLACK);

		BarRenderer renderer = new BarRenderer();
		renderer.setBarPainter(new StandardBarPainter());
		renderer.setShadowVisible(false);
		plot.setRenderer(renderer);
	}

}